package org.zhangyp.n02datastructure.n03Stack;

/**
 * 栈相关测试
 *
 * @author ypzhang
 * @since 2024/12/25
 */
public class Test {

    /**
     * 判断字符串是否括号匹配
     *
     * @param s
     * @return
     */
    public static boolean isOk(String s) {
        MyStack<Character> charStack = new MyArrayStack<>(20);
        char[] cs = s.toCharArray();
        Character top;
        for (char c : cs) {
            switch (c) {
                case '{':
                case '(':
                case '[':
                    charStack.push(c);
                    break;
                case '}':
                    top = charStack.pop();
                    if (top == null || top != '{') {
                        return false;
                    }
                    break;
                case ')':
                    top = charStack.pop();
                    if (top == null || top != '(') {
                        return false;
                    }
                    break;
                case ']':
                    top = charStack.pop();
                    if (top == null || top != '[') {
                        return false;
                    }
                    break;
                default:
                    break;
            }
        }
        return charStack.isEmpty();
    }

    public static void main(String[] args) {
        String s = "[(){()}{}]";
        System.out.println(isOk(s));
    }
}
